package zhongbo.java.air.dictionary.service.Impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.exception.ExceptionCode;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.model.enums.AircraftType;
import zhongbo.java.air.dictionary.model.enums.Status;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.Arrays;

@Service
@Slf4j
public class IAirCraftServiceImpl implements IAirCraftService{

    @Autowired
    AirCraftMapper airCraftMapper;

    /*
       *添加飞机信息
       * ICAO 24位地址码（唯一业务编号），不能重复，必填字段，字符不能超过六个字符
       * registration飞机注册号，必填项，不能超过20个字符
       * 所属航空公司不能够为空
       * 机型（枚举值示例：B737-800, A320-200）必填项
       * "状态（枚举值：ACTIVE-运营中, MAINTENANCE-维护中, RETIRED-已退役）不能为空，
       * 如果为已退役，
       * 添加成功
     */
    @Override
    public AirCraft insert(AirCraft airCraft) {
        //限制StrUtil.isBlank字符串是否仅包含空白字符（空格，制表符等），或不能为空
        //null对象为空----isEmpty检查字符串长度是否为0
        if(StrUtil.isBlank(airCraft.getIcaoCode()) ||
           StrUtil.isBlank(airCraft.getAirline()) ||
           StrUtil.isBlank(airCraft.getRegistration())||
           airCraft.getStatus()==null ||
           airCraft.getAircraftType()==null){
            throw new RuntimeException("飞机信息必填内容不能为空");
        }
        //长度
        if(airCraft.getIcaoCode().length()>6 ){
            throw new RuntimeException("icaoCode唯一的业务编码长度大于6,超过限制");
        }
        //限制飞机注册号小于20,
        if( airCraft.getRegistration().length()>20){
            throw new RuntimeException("飞机注册号registration大于20,超过限制");
        }

        //添加的状态枚举不对
        if(!Arrays.asList(Status.ACTIVE,Status.MAINTENANCE,Status.RETIRED).contains(airCraft.getStatus())){
            throw  new RuntimeException("状态不符合格式，选择：ACTIVE(运营中),MAINTENANCE(维护中),RETIRED(已退役)");
        }

        //机型枚举
        if(!Arrays.asList(AircraftType.A320_200,AircraftType.B737_800).contains(airCraft.getAircraftType())){
            throw new RuntimeException("飞机机型不符合格式，请输入A320_200,B737_800，获得请求新添加机型");
        }

        //构建查询条件icao_code为唯一性验证,或者registration飞机机型，不能重复
        QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("icao_code", airCraft.getIcaoCode());
        queryWrapper.or().eq("registration",airCraft.getRegistration());
        //selectOne--Mybatis-plus需要获取具体的记录数据，或者需要基于这条记录进行更新操作时
        //exist只需要判断记录是否存在，而不需要具体的记录数据时
        boolean existingAirCraft = airCraftMapper.exists(queryWrapper);
       //判断飞机信息是否存在,true
        if(existingAirCraft){
            throw new IllegalArgumentException("icao_code唯一业务编码重复,无法添加飞机信息");
        }

        // 业务规则：已退役状态处理
        if (Status.RETIRED.equals(airCraft.getStatus())) {
            log.info("已退役飞机登记: {}", airCraft.getIcaoCode());

        }

        //输入正确飞机信息，继续添加飞机信息
        int add = airCraftMapper.insert(airCraft);
         if(add>0){
             return airCraft;
         }
         throw new RuntimeException("添加飞机信息失败");
    }


    @Override
    public String delete(String id) {

        AirCraft airCraft =airCraftMapper.selectById(id);
        if(airCraft==null){
            throw new RuntimeException("飞机信息为空，删除失败");
        }

        airCraftMapper.deleteById(id);
        return id;
    }


    @Override
    public AirCraft select(String id){
        AirCraft airCraft =airCraftMapper.selectById(id);
        return airCraft;
    }

    //更新状态（枚举值：ACTIVE-运营中, MAINTENANCE-维护中, RETIRED-已退役）
    @Override
    public AirCraft updateStatus(AirCraft airCraft) {
        //查id
        AirCraft select =airCraftMapper.selectById(airCraft.getId());
        if(select==null){
            throw new RuntimeException("该飞机信息不存在，不能进行修改");
        }
        if(StrUtil.isBlank(airCraft.getIcaoCode()) ||
                StrUtil.isBlank(airCraft.getAirline()) ||
                StrUtil.isBlank(airCraft.getRegistration())||
                airCraft.getStatus()==null ||
                airCraft.getAircraftType()==null){
            throw new RuntimeException("飞机信息必填内容不能为空");
        }
        //状态枚举是否等于规定的飞机状态
        if(!Arrays.asList(Status.RETIRED,Status.MAINTENANCE,Status.ACTIVE).contains(airCraft.getStatus())){
           throw new BusinessException("状态不满足规定格式：RETIRED,MAINTENANCE,ACTIVE");
        }
        //判断输入的是否为空
        int dd = airCraftMapper.updateById(airCraft);

        if(dd>0){
            return airCraft;
        }
        return null;

    }

    //分页查询
    @Override
    public PageVo<AirCraft> findPage(String id, Integer page, Integer pageSize) {
        QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(id)) {
            queryWrapper.like("id", id);
        }
        IPage<AirCraft> pageParam = new Page<>(page, pageSize);
        IPage<AirCraft> airCraftIPage = airCraftMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(airCraftIPage);
    }



}
